﻿using Akka.Actor;
using Akka.Configuration;

class Program
{
    static void Main(string[] args)
    {
        {
            var config = ConfigurationFactory.ParseString(@"
                akka {
                    actor {
                        provider = remote
                    }
                    remote {
                        dot-netty.tcp {
                            port = 8081
                            hostname = localhost
                            maximum-frame-size = 10MiB
                        }
                    }
                }
            ");

            var system = ActorSystem.Create("ServerA", config);
            var serverAActor = system.ActorOf(Props.Create<ServerAActor>(), "serverAActor");

            // 向服务器 B 发送消息
            var serverBAddress = "akka.tcp://ServerB@localhost:8082/user/serverBActor";
            var serverBActor = system.ActorSelection(serverBAddress);

            //serverBActor.Tell("Hello from Server A!", serverAActor);
            serverBActor.Tell(new byte[1024 * 1024], serverAActor);
        }

        {
            var config = ConfigurationFactory.ParseString(@"
                akka {
                    actor {
                        provider = remote
                    }
                    remote {
                        dot-netty.tcp {
                            port = 8082
                            hostname = localhost
                            maximum-frame-size = 10MiB
                        }
                    }
                }
            ");

            var system = ActorSystem.Create("ServerB", config);
            var serverBActor = system.ActorOf(Props.Create<ServerBActor>(), "serverBActor");
        }        

        Console.ReadLine();
    }
}

public class ServerAActor : ReceiveActor
{
    public ServerAActor()
    {
        Receive<byte[]>(message =>
        {
            Console.WriteLine($"Server A received: {message}");

            // 回复消息给服务器 B
            var sender = Context.Sender;
            //sender.Tell("Hello from Server A!");
            sender.Tell(new byte[1024 * 1024]);
        });
    }
}

public class ServerBActor : ReceiveActor
{
    public ServerBActor()
    {
        Receive<byte[]>(message =>
        {
            Console.WriteLine($"Server B received: {message}");

            // 回复消息给服务器 A
            var sender = Context.Sender;
            //sender.Tell("Hello from Server B!");
            sender.Tell(new byte[1024 * 1024]);
        });
    }
}